#天猫和淘宝的详情页



#滚动
lastTop= -1
noScroll=400
soll = (x)->
	ret = ->
		if noScroll <= 0
			return off
		if lastTop == document.body.scrollTop
			noScroll--
		lastTop = document.body.scrollTop
		document.body.scrollTop += x
	ret


#开始
requestData=
	searchData:1
	mainViewTimeMin:1
	mainViewTimeMax:1
	otherViewTimeMin:1
	otherViewTimeMax:1
	viewTimeMin:1
	viewTimeMax:1
	probability:1
	clickedIds:1
	speed:1
chrome.extension.sendRequest requestData,(resp)->
	if resp and resp.searchData
		messenger.clickItem()
		maxTime = resp.mainViewTimeMax
		minTime = resp.mainViewTimeMin
		if resp.clickedIds and resp.clickedIds.length
			maxTime = resp.otherViewTimeMax
			minTime = resp.otherViewTimeMin
		viewTime = (maxTime-minTime)*Math.random()+minTime*1
		viewTime *= 60000
		console.debug viewTime
		viewTime = Math.max(viewTime,60000*1)
		messenger.start(viewTime/1000)
		#向下滚动
		setTimeout( ->
			time = 1
			for i in [1..30000]
				setTimeout(soll(1),time)
				time += 1-Math.ceil(Math.random()*resp.speed)
		,3000)
		seeTime = [viewTime/3,viewTime*2/3]
		seeTime.sort((a,b)->
			Math.random() - 0.5
		)
		#看评论
		setTimeout(->
			commentEl = document.querySelector('#J_TabBar li:nth-child(2) a')
			simulateClick commentEl
			noScroll=0
		,seeTime[0] + Math.ceil(Math.random()*20000))
		#看成交
		setTimeout(->
			dealEl = document.querySelector('#J_TabBar li:nth-child(3) a')
			simulateClick dealEl
			noScroll = 0
		,seeTime[1] + Math.ceil(Math.random()*10000))
		#浏览完成，点击下一页面或开始下一个浏览
		setTimeout( ->
			messenger.stop()
			messenger.postPage()
			if Math.random() < resp.probability[resp.clickedIds.length]/100
				#点击其它页面
				links = (img for img in document.querySelectorAll('a.side-item img,.ald-item a img'))
				len =links.length
				if len
					links.sort ->
						Math.random()-0.5
					oneImg = links[Math.floor(Math.random()*len)]
					for oneImg in links
						url = $(oneImg).parent().attr('href')
						itemId = messenger.getItemId(url)
						meId = messenger.getItemId location.href
						if itemId and itemId!=meId and itemId not in resp.clickedIds
							$(oneImg).closest('a').attr('target','_self')
							simulateClick oneImg,{ctrl:on}
							return off
			#下次开始
			messenger.next()
		,viewTime)
